﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类Tasks_BaseInfo。
    /// </summary>
    public class Tasks_BaseInfo
    {
        public Tasks_BaseInfo()
        { }
        #region  成员方法
                             
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Tasks_BaseInfo where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.Tasks_BaseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Tasks_BaseInfo(");
            strSql.Append("RecordId,relatedId,relatedCaption,relatedCode,taskReceiverId,taskReceiver,ReceiverDepartId,ReceiverDepart,TaskType,TaskBeginDay,TaskEndDay,Status,Title,Description,Addtime,AddPersonId,AddPerson,AddDepartId,AddDepart,AddCompanyId,AddCompany,CityId,CityName,DealDay,DealDescription)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@relatedId,@relatedCaption,@relatedCode,@taskReceiverId,@taskReceiver,@ReceiverDepartId,@ReceiverDepart,@TaskType,@TaskBeginDay,@TaskEndDay,@Status,@Title,@Description,@Addtime,@AddPersonId,@AddPerson,@AddDepartId,@AddDepart,@AddCompanyId,@AddCompany,@CityId,@CityName,@DealDay,@DealDescription)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "relatedId", DbType.Guid, model.relatedId);
            db.AddInParameter(dbCommand, "relatedCaption", DbType.AnsiString, model.relatedCaption);
            db.AddInParameter(dbCommand, "relatedCode", DbType.AnsiString, model.relatedCode);
            db.AddInParameter(dbCommand, "taskReceiverId", DbType.Guid, model.taskReceiverId);
            db.AddInParameter(dbCommand, "taskReceiver", DbType.AnsiString, model.taskReceiver);
            db.AddInParameter(dbCommand, "ReceiverDepartId", DbType.Guid, model.ReceiverDepartId);
            db.AddInParameter(dbCommand, "ReceiverDepart", DbType.AnsiString, model.ReceiverDepart);
            db.AddInParameter(dbCommand, "TaskType", DbType.AnsiString, model.TaskType);
            db.AddInParameter(dbCommand, "TaskBeginDay", DbType.DateTime, model.TaskBeginDay);
            db.AddInParameter(dbCommand, "TaskEndDay", DbType.DateTime, model.TaskEndDay);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "Title", DbType.AnsiString, model.Title);
            db.AddInParameter(dbCommand, "Description", DbType.AnsiString, model.Description);
            db.AddInParameter(dbCommand, "Addtime", DbType.DateTime, model.Addtime);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddDepartId", DbType.Guid, model.AddDepartId);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "AddCompanyId", DbType.Guid, model.AddCompanyId);
            db.AddInParameter(dbCommand, "AddCompany", DbType.AnsiString, model.AddCompany);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "DealDay", DbType.DateTime, model.DealDay);
            db.AddInParameter(dbCommand, "DealDescription", DbType.AnsiString, model.DealDescription);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.Tasks_BaseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Tasks_BaseInfo set ");
            strSql.Append("relatedId=@relatedId,");
            strSql.Append("relatedCaption=@relatedCaption,");
            strSql.Append("relatedCode=@relatedCode,");
            strSql.Append("taskReceiverId=@taskReceiverId,");
            strSql.Append("taskReceiver=@taskReceiver,");
            strSql.Append("ReceiverDepartId=@ReceiverDepartId,");
            strSql.Append("ReceiverDepart=@ReceiverDepart,");
            strSql.Append("TaskType=@TaskType,");
            strSql.Append("TaskBeginDay=@TaskBeginDay,");
            strSql.Append("TaskEndDay=@TaskEndDay,");
            strSql.Append("Status=@Status,");
            strSql.Append("Title=@Title,");
            strSql.Append("Description=@Description,");
            strSql.Append("Addtime=@Addtime,");
            strSql.Append("AddPersonId=@AddPersonId,");
            strSql.Append("AddPerson=@AddPerson,");
            strSql.Append("AddDepartId=@AddDepartId,");
            strSql.Append("AddDepart=@AddDepart,");
            strSql.Append("AddCompanyId=@AddCompanyId,");
            strSql.Append("AddCompany=@AddCompany,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("CityName=@CityName,");
            strSql.Append("DealDay=@DealDay,");
            strSql.Append("DealDescription=@DealDescription");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "relatedId", DbType.Guid, model.relatedId);
            db.AddInParameter(dbCommand, "relatedCaption", DbType.AnsiString, model.relatedCaption);
            db.AddInParameter(dbCommand, "relatedCode", DbType.AnsiString, model.relatedCode);
            db.AddInParameter(dbCommand, "taskReceiverId", DbType.Guid, model.taskReceiverId);
            db.AddInParameter(dbCommand, "taskReceiver", DbType.AnsiString, model.taskReceiver);
            db.AddInParameter(dbCommand, "ReceiverDepartId", DbType.Guid, model.ReceiverDepartId);
            db.AddInParameter(dbCommand, "ReceiverDepart", DbType.AnsiString, model.ReceiverDepart);
            db.AddInParameter(dbCommand, "TaskType", DbType.AnsiString, model.TaskType);
            db.AddInParameter(dbCommand, "TaskBeginDay", DbType.DateTime, model.TaskBeginDay);
            db.AddInParameter(dbCommand, "TaskEndDay", DbType.DateTime, model.TaskEndDay);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "Title", DbType.AnsiString, model.Title);
            db.AddInParameter(dbCommand, "Description", DbType.AnsiString, model.Description);
            db.AddInParameter(dbCommand, "Addtime", DbType.DateTime, model.Addtime);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddDepartId", DbType.Guid, model.AddDepartId);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "AddCompanyId", DbType.Guid, model.AddCompanyId);
            db.AddInParameter(dbCommand, "AddCompany", DbType.AnsiString, model.AddCompany);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "DealDay", DbType.DateTime, model.DealDay);
            db.AddInParameter(dbCommand, "DealDescription", DbType.AnsiString, model.DealDescription);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Tasks_BaseInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.Tasks_BaseInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,relatedId,relatedCaption,relatedCode,taskReceiverId,taskReceiver,ReceiverDepartId,ReceiverDepart,TaskType,TaskBeginDay,TaskEndDay,Status,Title,Description,Addtime,AddPersonId,AddPerson,AddDepartId,AddDepart,AddCompanyId,AddCompany,CityId,CityName,DealDay,DealDescription from Tasks_BaseInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.House.Entity.Tasks_BaseInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,relatedId,relatedCaption,relatedCode,taskReceiverId,taskReceiver,ReceiverDepartId,ReceiverDepart,TaskType,TaskBeginDay,TaskEndDay,Status,Title,Description,Addtime,AddPersonId,AddPerson,AddDepartId,AddDepart,AddCompanyId,AddCompany,CityId,CityName,DealDay,DealDescription ");
            strSql.Append(" FROM Tasks_BaseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize, int PageIndex, string OrderField, string TableName, string strWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.Tasks_BaseInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,relatedId,relatedCaption,relatedCode,taskReceiverId,taskReceiver,ReceiverDepartId,ReceiverDepart,TaskType,TaskBeginDay,TaskEndDay,Status,Title,Description,Addtime,AddPersonId,AddPerson,AddDepartId,AddDepart,AddCompanyId,AddCompany,CityId,CityName,DealDay,DealDescription ");
            strSql.Append(" FROM Tasks_BaseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.Tasks_BaseInfo> list = new List<Chenxin.Lib.House.Entity.Tasks_BaseInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.Tasks_BaseInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.Tasks_BaseInfo model = new Chenxin.Lib.House.Entity.Tasks_BaseInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["relatedId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.relatedId = new Guid(ojb.ToString());
            }
            model.relatedCaption = dataReader["relatedCaption"].ToString();
            model.relatedCode = dataReader["relatedCode"].ToString();
            ojb = dataReader["taskReceiverId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.taskReceiverId = new Guid(ojb.ToString());
            }
            model.taskReceiver = dataReader["taskReceiver"].ToString();
            ojb = dataReader["ReceiverDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReceiverDepartId = new Guid(ojb.ToString());
            }
            model.ReceiverDepart = dataReader["ReceiverDepart"].ToString();
            model.TaskType = dataReader["TaskType"].ToString();
            ojb = dataReader["TaskBeginDay"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TaskBeginDay = (DateTime)ojb;
            }
            ojb = dataReader["TaskEndDay"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TaskEndDay = (DateTime)ojb;
            }
            ojb = dataReader["Status"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Status = (int)ojb;
            }
            model.Title = dataReader["Title"].ToString();
            model.Description = dataReader["Description"].ToString();
            ojb = dataReader["Addtime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Addtime = (DateTime)ojb;
            }
            ojb = dataReader["AddPersonId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddPersonId = new Guid(ojb.ToString());
            }
            model.AddPerson = dataReader["AddPerson"].ToString();
            ojb = dataReader["AddDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddDepartId = new Guid(ojb.ToString());
            }
            model.AddDepart = dataReader["AddDepart"].ToString();
            ojb = dataReader["AddCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddCompanyId = new Guid(ojb.ToString());
            }
            model.AddCompany = dataReader["AddCompany"].ToString();
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            model.CityName = dataReader["CityName"].ToString();
            ojb = dataReader["DealDay"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DealDay = (DateTime)ojb;
            }
            ojb = dataReader["DealDescription"];
            model.DealDescription = dataReader["DealDescription"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

